package com.tuling.dao;

import com.tuling.Util.DruidUtils;
import com.tuling.domain.Fruit;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.SQLException;
import java.util.List;

public class FruitDaoImpl implements FruitDao {
    private QueryRunner queryRunner = new QueryRunner(DruidUtils.getDataSource());
    @Override
    public List<Fruit> getHot() {
        String sql = "select t2.* from hotfruits t1, fruits t2 WHERE t1.fid = t2.fid";
        try {
            List<Fruit> query = queryRunner.query(sql, new BeanListHandler<Fruit>(Fruit.class));
            return query;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public Fruit getById(int fid) {
        String sql = "SELECT * from fruits WHERE fid = ?";
        Object[] params = {fid};
        try {
            Fruit query = queryRunner.query(sql, new BeanHandler<Fruit>(Fruit.class), params);
            return query;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public List<Fruit> getByKey(String key) {
        String sql = "select * from fruits where fname like ?";
        Object[] params = {"%"+key+"%"};
        try {
            List<Fruit> fruits = queryRunner.query(sql, new BeanListHandler<>(Fruit.class), params);
            return fruits;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public List<Fruit> getAll() {
        String sql = "select * from fruits";
        try {
            List<Fruit> query = queryRunner.query(sql, new BeanListHandler<>(Fruit.class));
            return query;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public int add(Fruit fruit) {
        String sql = "INSERT INTO fruits (fname, spec, up, t1, t2, inum) values (?, ?, ?, ?, ?, ?)";
        Object[] params = {fruit.getFname(), fruit.getSpec(), fruit.getUp(), fruit.getT1(), fruit.getT2(), fruit.getInum()};
        try {
            int update = queryRunner.update(sql, params);
            return update;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public int update(Fruit fruit) {
        String sql = "UPDATE fruits SET fname =?, spec =?, up =?, t1 =?, t2 =?, inum =? WHERE fid =?";
        Object[] params = {fruit.getFname(), fruit.getSpec(), fruit.getUp(), fruit.getT1(), fruit.getT2(), fruit.getInum(), fruit.getFid()};
        try {
            int update = queryRunner.update(sql, params);
            return update;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public int del(int fid) {
        String sql = "DELETE FROM fruits WHERE fid = ?";
        Object[] params = {fid};
        try {
            int update = queryRunner.update(sql, params);
            return update;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }
}
